/*
 * Copyright (c) 2023-2025 110476592@qq.com
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

/**
 * @file pen_picker.cpp
 * @brief 截图工具 
 * @author 110476592@qq.com 
 * @note   承接Qt项目外包定制开发。长期接收C++项目实训学徒,联系QQ：110476592。
 */

 
#include "pen_picker.h"
#include <QDebug>
#include <QEvent>
#include <QHBoxLayout>
#include <QPainter>
#include <QRect>
#include <QScreen>
#include <QVBoxLayout>
#include <QVariant>
#include <common/draw_context.h>
#include <common/global.h>
#include <tools/pen_item.h>
#include <widgets/toolbuttonex.h>

PenPicker::PenPicker(QWidget *parent) : ElColorPicker(parent) {
  setProperty("form", "absdialog");
  createWidgets();
  initlization();
  initlizationSolts();
  updateLayout();
}

PenPicker::~PenPicker() { qDebug() << "~PenPicker()"; }

void PenPicker::createWidgets() {
  m_penContainer = new QWidget(this);
  m_penLayout = new QHBoxLayout(this);
  m_penGroup = new QButtonGroup(this);
}

void PenPicker::initlization() {
  m_penContainer->setFixedHeight(40);
  m_penContainer->setObjectName(QString::fromUtf8("penContainer"));

  m_penLayout->setSpacing(0);
  m_penLayout->setContentsMargins(0, 0, 0, 20);
  m_penLayout->setObjectName(QString::fromUtf8("horizontalLayout"));
  m_penContainer->setLayout(m_penLayout);

  QVector<int> penSizes = {2, 4, 8, 12, 16};
  for (int index = 0; index < penSizes.size(); ++index) {
    PenItem *btn = new PenItem(this);

    btn->setPenWidth(penSizes[index]);
    btn->setMaxPenWidth(16);
    m_penGroup->addButton(btn);
    btn->setFixedWidth(32);
    btn->setFixedHeight(32);
    btn->setObjectName(QString::number(penSizes[index]));
    btn->setCheckable(true);
    m_penLayout->addWidget(btn);
    connect(btn, &PenItem::clicked, this, &PenPicker::onSetPen);
  }
}

void PenPicker::initlizationSolts() {
  connect(this, &PenPicker::colorSelected, this, [this](const QColor &color) {
    auto penList = m_penGroup->buttons();
    foreach (auto btn, penList) {
      PenItem *pen = static_cast<PenItem *>(btn);
      pen->setColor(color);
      pen->repaint();
    }

    if (this->m_triggerBtn) {
      ToolButtonEx *orgBtn = static_cast<ToolButtonEx *>(m_triggerBtn);
      int fontCode = orgBtn->getFontId();
      bool ok = true;

      ushort num = QString::number(fontCode, 16).toUShort(&ok, 16);
      DrawContext::setColor(color);
      el::ELIconUtil::setIcon(orgBtn, QChar(num), color.name(),
                              DrawContext::penSize() * 2);
    }

     emit penSelected();
  });
}

void PenPicker::updateLayout() {
  QLayout *layoutMain = this->layout();
  layoutMain->addWidget(m_penContainer);
}

PenPicker &PenPicker::showPicker(QWidget *parent, ELPosition position,
                                 const QSize &size) {
  PenPicker *pop = new PenPicker(parent);
  pop->showPop(parent, position, size,DrawContext::color());
  pop->m_triggerBtn = parent;
  return *pop;
}

void PenPicker::onSetPen() {
  PenItem *btn = static_cast<PenItem *>(sender());
  btn->setChecked(true);

  int penWidth = btn->getPenWidth();
  DrawContext::setPenSize(penWidth);

  if (this->m_triggerBtn) {
    ToolButtonEx *orgBtn = static_cast<ToolButtonEx *>(m_triggerBtn);
    int fontCode = orgBtn->getFontId();
    bool ok = true;

    ushort num = QString::number(fontCode, 16).toUShort(&ok, 16);
    el::ELIconUtil::setIcon(orgBtn, QChar(num), DrawContext::color().name(),
                            penWidth * 2);
  }

  emit penSelected();
  close();
}
